{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time  # 导入系统库time，可以使用一些系统级别的函数\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def buy_one_stock(stock_name='sh600000'):  # 参数为股票名\n",
    "    \"\"\"\n",
    "    此程序用于下单买入某个股票，但是买入过程中，程序有50%的概率报错。\n",
    "    \"\"\"\n",
    "    r = random.random()\n",
    "    if r >= 0.5:\n",
    "        print(r, '购买成功')\n",
    "        return\n",
    "    else:        \n",
    "        print(r, '购买失败')\n",
    "        raise ValueError('程序报错！')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.39555793212451973 购买失败\n",
      "警告！下单出错，停止1秒再尝试\n",
      "0.5134079330317293 购买成功\n",
      "行了，下单成功了\n"
     ]
    }
   ],
   "source": [
    "max_try_num = 5\n",
    "tyr_num = 0\n",
    "\n",
    "\n",
    "while True:\n",
    "    try:  # 尝试做以下事情\n",
    "        buy_one_stock()\n",
    "    except:  # 如果因为各种原因报错\n",
    "        print('警告！下单出错，停止1秒再尝试')\n",
    "        tyr_num += 1\n",
    "        time.sleep(1)\n",
    "        if tyr_num > max_try_num:\n",
    "            print('超过最大尝试次数，下单失败')\n",
    "            # 此处需要执行相关程序，通知某些人\n",
    "            break\n",
    "    else:  # 如果没有报错\n",
    "        print('行了，下单成功了')\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "执行相关语句1\n",
      "执行相关语句3\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    print('执行相关语句1')\n",
    "except:\n",
    "    print('执行相关语句2')\n",
    "    raise ValueError('程序报错，买入异常')\n",
    "else:\n",
    "    print('执行相关语句3')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "程序报错，买入异常",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-0c2f91ba664d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'程序报错，买入异常'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: 程序报错，买入异常"
     ]
    }
   ],
   "source": [
    "raise ValueError('程序报错，买入异常')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
